package com.pan.games.leecode;

import org.springframework.util.StringUtils;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/3/24
 */
public class AddTwoNumbers {

    public static void main(String[] args) {
        AddTwoNumbers a = new AddTwoNumbers();
        ListNode l1 = new ListNode();
        l1.val=1;
        l1.next=new ListNode(2,new ListNode(3,null));
        a.addTwoNumbers(l1,l1);
    }
    public void addTwoNumbers(ListNode l1, ListNode l2) {

        String num1 = getNumFromNode2(l1);
        String num2 = getNumFromNode2(l2);
        int sum = Integer.valueOf(num1)+Integer.valueOf(num2);
        String sumstr = String.valueOf(sum);
        String reversStr = reverseStr(sumstr);
        char[] chars = reversStr.toCharArray();
        ListNode ls = new ListNode(chars[0]);
        for (int i = 0; i < chars.length; i++) {
            ListNode next = new ListNode(chars[0]);
            ls.next=next;
            next=ls;
        }
    }


    private String getNumFromNode2(ListNode l1) {
        String l1start = String.valueOf(l1.val);
        while(l1.next!=null){
            ListNode next = l1.next;
            l1start = l1start+String.valueOf(next.val);
            l1 = next;
        }
        return reverseStr(l1start);
    }

    public static String reverseStr(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; i--) {
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}



 class ListNode {
     int val;
     ListNode next;
     ListNode() {}

     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }

}
